package org.fmi.lje.uml.awt;

import java.awt.Dimension;
import java.io.StringWriter;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFrame;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.batik.swing.JSVGCanvas;
import org.fmi.lje.graphics.diagrams.ClassHierarchyDiagram;
import org.fmi.lje.graphics.model.Clazz;
import org.fmi.lje.modules.AntProjectCompilerModule;
import org.fmi.lje.modules.AntProjectLoaderModule;
import org.fmi.lje.modules.BcelModule;
import org.fmi.lje.uml.Diagram;
import org.fmi.lje.uml.DiagramBuilder;
import org.fmi.lje.uml.svg.SvgDiagram;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class AwtDiagramBuilder implements DiagramBuilder {
	private final static Injector injector = Guice.createInjector(new AntProjectCompilerModule(), new AntProjectLoaderModule(),
			new BcelModule());

	@Override
	public SvgDiagram buildDiagram(Class<?> clazz) {
		ClassHierarchyDiagram diagram = injector.getInstance(ClassHierarchyDiagram.class);
		diagram.setSVGCanvasSize(new Dimension(7000, 3000));

		Set<Clazz> allClasses = new HashSet<Clazz>();

		Clazz parent = diagram.createShape(clazz);

		allClasses.add(parent);
		allClasses.addAll(getSuperClasses(parent));
		allClasses.addAll(getSubClasses(parent));

		diagram.addAll(allClasses);
		for(Clazz clss : allClasses) {
			diagram.addAll(clss.getSubclasses());
//			diagram.addAll(clss.getSuperclasses());
		}
		diagram.getLayoutManager().addAll(allClasses);

		return diagram;
	}

	private Collection<? extends Clazz> getSubClasses(Clazz parent) {
		return parent.getSubclassHierarchy();
	}

	private Collection<? extends Clazz> getSuperClasses(Clazz parent) {
		return parent.getSuperClassHierarchy();
	}

	int lol(int x) {
		return x;
	}

	public static void main(String[] args) {
		AwtDiagramBuilder instance = new AwtDiagramBuilder();
		SvgDiagram diagram = instance.buildDiagram(Diagram.class);

		diagram.draw();

		// Populate the document root with the generated SVG content.
		Element root = diagram.getDocumentElement();
		diagram.getRoot(root);

		// Display the document.
		JSVGCanvas canvas = new JSVGCanvas();
		JFrame f = new JFrame();
		f.getContentPane().add(canvas);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		canvas.setSVGDocument(diagram.getDocument());
		
		System.out.println(xmlToString(diagram.getDocument()));
		f.pack();
		f.setVisible(true);
	}
	
	public static String xmlToString(Node node) {
        try {
            Source source = new DOMSource(node);
            StringWriter stringWriter = new StringWriter();
            Result result = new StreamResult(stringWriter);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.transform(source, result);
            return stringWriter.getBuffer().toString();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return null;
    }
}
